﻿using Microsoft.Win32;
using System;
using System.Configuration;
using System.IO;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace ZLSoft.ZLDocEditorEvents.Comm
{
    internal class Init
    {
        //机构代码
        public static string OrgCode;
        //服务地址
        public static string ServerUrl;
        //系统名称
        public static string ProductCode;
        //VTE程序名称
        public static string ExeName;
        //病历事件代码转换
        public static string DocConvert;
        //病历书写事件转换
        public static string EventConvert;
        //VTE卡控程序路径
        public static string ExeFullPath;
        //用户信息
        public static string UserInfo;
        //登录操作员
        public static string UserName;
        //调用卡控
        public static string CallVte;
        /// <summary>
        /// 数据库连接
        /// </summary>
        public static string Conn;
        public static bool SaveLog;
        public static string gstrSql;

        private static string fileName;
        public static bool InitParameter()
        {
            try
            {
                Init.fileName = Path.Combine(Init.AssemblyDirectory, "VteConfig.config");
                //Init.ProductCode = Init.GetSetting("ProductCode");
                //Init.ExeName = Init.GetSetting("ExeName");
                //string path = GetExePath(Init.ExeName);
                //if (!string.IsNullOrWhiteSpace(path)) Init.ExeFullPath = Path.Combine(path, Init.ExeName);
                //Init.DocConvert = Init.GetSetting("DocConvert");
                //Init.EventConvert = Init.GetSetting("EventConvert");
                Init.SaveLog = Func.Nvl(Init.GetSetting("SaveLog")).ToUpper().Equals("TRUE");
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "参数初始化", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return false;
            }
        }

        /// <summary>
        /// 获取Dll文件路径
        /// </summary>
        public static string AssemblyDirectory
        {
            get
            {
                string codeBase = Assembly.GetExecutingAssembly().CodeBase;
                UriBuilder uriBuilder = new UriBuilder(codeBase);
                string path = Uri.UnescapeDataString(uriBuilder.Path);
                return Path.GetDirectoryName(path);
            }
        }

        /// <summary>
        /// 获取配置文件值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        private static string GetSetting(string key)
        {
            string result = "";
            ConfigXmlDocument configXmlDocument = new ConfigXmlDocument();
            configXmlDocument.Load(Init.fileName);
            foreach (object obj in configXmlDocument.SelectSingleNode("configuration"))
            {
                XmlNode xmlNode = (XmlNode)obj;
                bool flag = xmlNode.Name == "appSettings";
                if (flag)
                {
                    XmlNode xmlNode2 = xmlNode.SelectSingleNode("add[@key='" + key + "']");
                    result = xmlNode2.Attributes["value"].Value.ToString();
                }
            }
            return result;
        }

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="src"></param>
        /// <returns></returns>
        internal static string Decrypt(string src)
        {
            bool flag = src == "";
            string result;
            if (flag)
            {
                result = src;
            }
            else
            {
                Encoding unicode = Encoding.UTF8;
                byte[] input = Convert.FromBase64String(src);
                byte[] bytes = Init.Decrypt(input);
                string @string = unicode.GetString(bytes);
                result = @string;
            }
            return result;
        }

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        private static byte[] Decrypt(byte[] input)
        {
            byte[] rgbKey = new byte[] { 19, 144, 17, 153, 147, 21, 51, 25 };
            byte[] rgbIV = new byte[] { 8, 17, 65, 57, 7, 25, 153, 49 };
            DESCryptoServiceProvider descryptoServiceProvider = new DESCryptoServiceProvider();
            ICryptoTransform cryptoTransform = descryptoServiceProvider.CreateDecryptor(rgbKey, rgbIV);
            return cryptoTransform.TransformFinalBlock(input, 0, input.Length);
        }

        /// <summary>
        /// 获取文件路径
        /// </summary>
        /// <returns></returns>
        public static string GetExePath(string ExeName)
        {
            RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\" + ExeName);
            return Func.Nvl(key?.GetValue("path", "").ToString(), "");
        }
    }
}
